package jadx.gui.treemodel;

import jadx.api.ICodeInfo;
import jadx.api.JadxDecompiler;
import jadx.api.JavaNode;
import jadx.gui.ui.TabbedPane;
import jadx.gui.ui.panel.ContentPanel;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

public abstract class JNode extends DefaultMutableTreeNode {

	private static final long serialVersionUID = -5154479091781041008L;

	public abstract JClass getJParent();

	/**
	 * Return top level JClass or self if already at top.
	 */
	public JClass getRootClass() {
		return null;
	}

	public JavaNode getJavaNode() {
		return null;
	}

	public String getContent() {
		return null;
	}

	@Nullable
	public ContentPanel getContentPanel(TabbedPane tabbedPane) {
		return null;
	}

	public String getSmali() {
		return null;
	}

	public String getSyntaxName() {
		return SyntaxConstants.SYNTAX_STYLE_NONE;
	}

	public int getLine() {
		return 0;
	}

	@Nullable
	public ICodeInfo getCodeInfo() {
		return null;
	}

	public final Integer getSourceLine(int line) {
		ICodeInfo codeInfo = getCodeInfo();
		if (codeInfo == null) {
			return null;
		}
		return codeInfo.getLineMapping().get(line);
	}

	@Nullable
	public JavaNode getJavaNodeAtPosition(JadxDecompiler decompiler, int line, int offset) {
		ICodeInfo codeInfo = getCodeInfo();
		if (codeInfo == null) {
			return null;
		}
		/*
		当设置IDEA L&F 时， 后台疯狂报错， 死循环
	at com.intellij.openapi.application.impl.ApplicationImpl.runIntendedWriteActionOnCurrentThread(ApplicationImpl.java:822)
	at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:507)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
jadx.core.utils.exceptions.JadxRuntimeException: JavaClass not found by ClassNode: me.weishu.epic.art.Epic
	at jadx.api.JadxDecompiler.getJavaClassByNode(JadxDecompiler.java:431)
	at jadx.api.JadxDecompiler.getJavaMethodByNode(JadxDecompiler.java:441)
	at jadx.api.JadxDecompiler.convertNode(JadxDecompiler.java:534)
	at jadx.api.JadxDecompiler.getJavaNodeAtPosition(JadxDecompiler.java:567)
	at jadx.gui.treemodel.JNode.getJavaNodeAtPosition(JNode.java:80)
	at jadx.gui.ui.codearea.CodeArea.getJavaNodeAtOffset(CodeArea.java:203)
	at jadx.gui.ui.codearea.JadxTokenMaker.mergeLongClassNames(JadxTokenMaker.java:65)
	at jadx.gui.ui.codearea.JadxTokenMaker.processTokens(JadxTokenMaker.java:46)
	at jadx.gui.ui.codearea.JadxTokenMaker.getTokenList(JadxTokenMaker.java:31)
	at org.fife.ui.rsyntaxtextarea.RSyntaxDocument.getTokenListForLine(RSyntaxDocument.java:431)
		 */
		try {
			return decompiler.getJavaNodeAtPosition(codeInfo, line, offset);
		} catch (Throwable e) {
			e.printStackTrace();
			return null;
		}
	}

	public abstract Icon getIcon();

	public String getName() {
		JavaNode javaNode = getJavaNode();
		if (javaNode == null) {
			return null;
		}
		return javaNode.getName();
	}

	public boolean canRename() {
		return false;
	}

	public abstract String makeString();

	public String makeStringHtml() {
		return makeString();
	}

	public String makeDescString() {
		return null;
	}

	public boolean hasDescString() {
		return false;
	}

	public String makeLongString() {
		return makeString();
	}

	public String makeLongStringHtml() {
		return makeLongString();
	}

	public int getPos() {
		JavaNode javaNode = getJavaNode();
		if (javaNode == null) {
			return -1;
		}
		return javaNode.getDefPos();
	}

	public String getTooltip() {
		return null;
	}

	@Override
	public String toString() {
		return makeString();
	}
}
